<script setup lang='ts'>
import { onMounted, ref, watch } from 'vue';
import { RouterLink, useRoute } from "vue-router";
import { useRouter } from "vue-router";
import { ElNotification } from 'element-plus'
import { login, userLogin } from "../../api/api";
const centerDialogVisible = ref(false)
const route = useRoute()
const router = useRouter()
const thisRouter = ref()
const islogin = ref(false)
const mail = ref('')
const type = ref()
const frequency = ref(0)
const mailer = ref()
const password = ref('')
watch(() => route.query.isrouter, (value) => {
    thisRouter.value = value as any
})
watch(() => localStorage.getItem('mail'), (value) => {
    mailer.value = value as any
})
onMounted(() => {
    thisRouter.value = route.query.isrouter as string
    ifLogin()
})
//跳转路由
const getdata = () => {
    router.push({ name: 'ThisData', query: { type: type.value } })
}
//判断是否登录
const ifLogin = async () => {
    const res = await userLogin({ _id: localStorage.getItem('mail') as string })
    if (res.data.code === '404') {
        localStorage.setItem('token', '')
        localStorage.setItem('mail', '')
        islogin.value = false
        return
    }
    mailer.value = res.data.data.mail
    frequency.value = res.data.data.frequency
    islogin.value = true
}
//登录
const loginData = async () => {
    if (mail.value.length === 0 && password.value.length === 0) return open4('错误', '请输入内容','error')
    const res = await login({ mail: mail.value, password: password.value })
    if (res.data.code !== '200') return open4('错误', res.data.msg,'error')
    localStorage.setItem('token', res.data.data.token)
    localStorage.setItem('mail', res.data.data.id)
    centerDialogVisible.value = false
    islogin.value = true
    open4('欢迎您', '登录成功','success')
    ifLogin()
}
//提示
const open4 = (title: string, text: string,typeData:any) => {
    ElNotification({
        title: title,
        message: text,
        type: typeData,
    })
}
</script>

<template>
    <el-dialog v-model="centerDialogVisible" :show-close="false" title="动画素材网登录" width="500" align-center>
        <el-form label-width="auto">
            <el-form-item label="邮箱：">
                <el-input v-model="mail" placeholder="请输入账号" />
            </el-form-item>
            <el-form-item label="密码：">
                <el-input v-model="password" type="password" placeholder="请输入密码" />
            </el-form-item>
            <RouterLink style="position: relative;left: 400px;" :to="{ path: 'register/reg' }">注册账号</RouterLink>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="centerDialogVisible = false">取消</el-button>
                <el-button type="primary" @click="loginData">
                    登录
                </el-button>
            </div>
        </template>
    </el-dialog>
    <!-- - -->
    <div class="contentPC">
        <div class="data">
            <ul>
                <div class="logo">
                    <img class="mylogo" src="../../../public/logo.png" alt="">
                </div>
                <RouterLink :to="{ name: 'home', query: { isrouter: 0 } }">
                    <li :class="thisRouter === '0' ? 'color' : ''">首页</li>
                </RouterLink>
                <RouterLink :to="{ name: 'select', query: { num: 0, isrouter: 1 } }">
                    <li :class="thisRouter === '1' ? 'color' : ''">素材库</li>
                </RouterLink>
                <RouterLink :to="{ name: 'uuserData', query: { isrouter: 2 } }">
                    <li :class="thisRouter === '2' ? 'color' : ''">个人中心</li>
                </RouterLink>
                <RouterLink :to="{ name: 'Buy', query: { isrouter: 3 } }">
                    <li :class="thisRouter === '3' ? 'color' : ''">积分充值</li>
                </RouterLink>
            </ul>
            <div class="input">
                <div class="input-container">
                    <input placeholder="请输入你想搜索的素材" v-model="type" type="email">
                    <button class="invite-btn" @click="getdata" type="button">
                        搜索
                    </button>

                </div>
            </div>
            <div class="login">
                <el-button color="#626aef" round v-if="!islogin" @click="centerDialogVisible = true">登录</el-button>
                <el-popover placement="top-start" :title="mailer" :width="200" trigger="hover" v-if="islogin"
                    :content="'您的积分还存有：' + frequency">
                    <template #reference>
                        <img style="width: 40%;" src="../../assets/t.png" alt="">
                    </template>
                </el-popover>

            </div>
        </div>
    </div>
</template>

<style scoped>
.contentPC {
    width: 100%;
    height: 60px;
    position: fixed;
    top: 0;
    background-color: rgba(255, 255, 255, 0.904);
    filter: blur(0.3px);
    z-index: 1;
    /* 这里的5px是模糊半径，可以根据需要调整 */
}

.data {
    max-width: 1500px;
    height: 100%;
    margin: auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

ul {
    width: 40%;
    height: 100%;
    box-sizing: border-box;
    padding: 8px;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 5px;
}

a {
    width: 100%;
    color: black;
    height: 100%;
    margin-left: 20px;
}
.mylogo{
    width: 100%;
}
li,
.logo {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 10px;
}

li {
    transition: background-color 0.8s ease, color 0.8s ease;
}

li:hover {
    background-color: #545c63;
    color: white
}

.color {
    background-color: #545c63;
    color: white
}

.login {
    height: 100%;
    display: flex;
    text-align: center;
    align-items: center;
}

.demo-fit .block {
    flex: 1;
    display: flex;
    flex-direction: column;
    flex-grow: 0;
}

.demo-fit .title {
    margin-bottom: 10px;
    font-size: 14px;
    color: var(--el-text-color-secondary);
}


/* From Uiverse.io by Yaya12085 */
.input-container {
    position: relative;
    display: flex;
    height: 2.8rem;
    width: 100%;
    height: 80%;
    max-width: 250px;
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 20px 20px 30px rgba(0, 0, 0, .05);
}

.input-container input {
    height: 100%;
    width: 100%;
    border-radius: 8px;
    border: 1px solid rgb(176 190 197);
    background-color: transparent;
    padding: 0.625rem 70px 0.625rem 0.75rem;
    font-size: 0.875rem;
    line-height: 1.25rem;
    font-weight: 400;
    color: rgb(69 90 100);
    outline: none;
    transition: all .15s cubic-bezier(0.4, 0, 0.2, 1);
}

.input-container input:focus {
    border: 1px solid rgb(236 72 153);
}

.invite-btn {
    position: absolute;
    width: 65px;
    right: 4px;
    top: 4px;
    bottom: 4px;
    z-index: 10;
    border-radius: 4px;
    background-color: rgb(236 72 153);
    color: #fff;
    padding-top: .25rem;
    padding-bottom: .25rem;
    padding-left: 0.5rem;
    padding-right: 0.5rem;
    text-align: center;
    vertical-align: middle;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    border: none;
    transition: .6s ease;
}

.invite-btn:hover {
    right: 2px;
    top: 2px;
    bottom: 2px;
    border-radius: 8px;
}

.input-container input:placeholder-shown~.invite-btn {
    pointer-events: none;
    background-color: gray;
    opacity: 0.5;
}
</style>